#import "OmCheckMailVC.h"
#import "OmCommonField0.h"
#import "OmCommonButton0.h"
#import "OmCommonButton1.h"
@interface OmCheckMailVC ()
{
    NSInteger _currentTimer;
}
@property (nonatomic, strong) OmCommonField0 *mailField;
@property (nonatomic, strong) OmCommonField0 *checkCodeField;
@property (nonatomic, strong) OmCommonButton0 *submitButton;
@property (nonatomic, strong) OmCommonButton1 *sendCheckMailButton;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation OmCheckMailVC
#pragma mark - Event
- (void)onSubmitButtonTap {
    if (![self isDataCorrect]) return;
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"verify_mail"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"mail"] = self.mailField.text;
    param[@"vcode"] = self.checkCodeField.text;
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        [MBProgressHUD showSuccessMessage:ASLocalizedString(@"修改成功")];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSString *error) {
        if(StrValid(error))
            [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
}
- (void)onSendCodeTap {
    if (!StrValid(self.mailField.text)) {
        [MBProgressHUD showInfoMessage:ASLocalizedString(@"请完整邮箱信息")];
        return;
    }
    [self.mailField resignFirstResponder];
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"send_mail"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"mail"] = self.mailField.text;
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        [MBProgressHUD showSuccessMessage:ASLocalizedString(@"发送成功")];
        [self.checkCodeField becomeFirstResponder];
        [self startTimer];
    } failure:^(NSString *error) {
        if(StrValid(error))
            [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
}
- (BOOL)isDataCorrect {
    if (!StrValid(self.mailField.text) || !StrValid(self.checkCodeField.text)) {
        [MBProgressHUD showInfoMessage:ASLocalizedString(@"请完整输入信息")];
        return NO;
    }
    return YES;
}
- (void)startTimer {
    _currentTimer = 60;
    self.sendCheckMailButton.userInteractionEnabled = NO;
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeSecound) userInfo:nil repeats:YES];
    }else {
        [self.timer setFireDate:[[NSDate date]dateByAddingTimeInterval:1]];
    }
}
- (void)changeSecound {
    _currentTimer --;
    if (_currentTimer <= 0) {
        if (_timer && [_timer isValid]) {
            [_timer invalidate];
            _timer = nil;
        }
        self.sendCheckMailButton.userInteractionEnabled = YES;
        [self.sendCheckMailButton setTitle:ASLocalizedString(@"发送") forState:UIControlStateNormal];
    }else {
        [self.sendCheckMailButton setTitle:NSStringFormat(@"%ld",_currentTimer) forState:UIControlStateNormal];
    }
}
#pragma mark - ConstructionScript
- (void)configSelf {
    [self setHiddenForTabBar:YES navigationBar:NO];
    self.title = ASLocalizedString(@"邮箱验证");
}
- (void)allocateComponents {
    self.mailField = [[OmCommonField0 alloc] initWithPlaceHolder:ASLocalizedString(@"请输入注册邮箱")];
    [self.view addSubview:self.mailField];
    self.sendCheckMailButton = [[OmCommonButton1 alloc] initWithTitle:ASLocalizedString(@"发送")];
    [self.sendCheckMailButton addTarget:self action:@selector(onSendCodeTap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sendCheckMailButton];
    self.checkCodeField = [[OmCommonField0 alloc] initWithPlaceHolder:ASLocalizedString(@"请输入验证码")];
    [self.view addSubview:self.checkCodeField];
    self.submitButton = (OmCommonButton0 *)[[OmCommonButton0 alloc] initWithTitle:ASLocalizedString(@"完成")]
    .fp_addTargetActionForControlEvents(self, @selector(onSubmitButtonTap), UIControlEventTouchUpInside);
    [self.view addSubview:self.submitButton];
}
- (void)layoutComponents {
    [self.mailField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(33));
        make.height.equalTo(@(RPX(28)));
        make.left.equalTo(self.checkCodeField.mas_left);
        make.right.equalTo(self.view).offset(RPX(-95));
    }];
    [self.sendCheckMailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(30));
        make.right.equalTo(self.view).offset(RPX(-15));
    }];
    [self.checkCodeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.mailField.mas_bottom).offset(RPX(28));
    }];
    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.checkCodeField.mas_bottom).offset(RPX(40));
    }];
}
@end
